package com.agy.filemanager.controller;

import com.agy.filemanager.common.TreeUtil;
import com.agy.filemanager.entity.DcBaseMenu;
import com.agy.filemanager.entity.TreeModel;
import com.agy.filemanager.service.DcSystemBaseMenuService;
import com.agy.filemanager.toolClass.Result;
import com.agy.filemanager.toolClass.ResultUtil;
import com.github.pagehelper.Page;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统设置-菜单维护
 */
@Controller
@RequestMapping("/systemManagerInfo")
public class SystemBaseMenuController {

    /**
     * 跳转至系统设置-菜单维护页面
     * @return
     */
    @Autowired
    private DcSystemBaseMenuService dcSystemBaseMenuService;

    /**
     * 分页-获取菜单信息
     * @param request
     * @return
     */
    @PostMapping("/menuInfo")
    @ResponseBody
    public Map<String,Object> menuInfo(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("pageSize"));
        int pageNum =Integer.parseInt(request.getParameter("pageNum"));
        String searchName = request.getParameter("searchName");
        String menuId = request.getParameter("menuId");
        Page list = this.dcSystemBaseMenuService.menuInfo(pageNum,pageSize,searchName,menuId);
        Map<String,Object> map=new HashMap<>();
        map.put("total",list.getTotal());
        map.put("rows",list.getResult());
        return map;
    }

    /**
     * 获取菜单树
     * @return
     */
    @PostMapping("/menuTreeInfo")
    @ResponseBody
    public Result agyOrganiseInfo(){
        List<TreeModel> list = TreeUtil.getTree(this.dcSystemBaseMenuService.menuTreeInfo());
        return ResultUtil.success(list, "成功");
    }

    /**
     * 新增菜单
     * @return
     */
    @PostMapping("/addMenu")
    @ResponseBody
    public Result addMenu(@RequestBody DcBaseMenu menu){
        try{
        this.dcSystemBaseMenuService.addMenu(menu);
        return ResultUtil.success(null, "新增成功");
        }catch(Exception ex){
            return ResultUtil.error("新增失败");
        }
    }

    /**
     * 修改菜单
     * @return
     */
    @PostMapping("/editMenu")
    @ResponseBody
    public Result editMenu(@RequestBody DcBaseMenu menu){
        try{
            this.dcSystemBaseMenuService.editMenu(menu);
            return ResultUtil.success(null, "修改成功");
        }catch(Exception ex){
            return ResultUtil.error("修改失败");
        }
    }

    /**
     * 禁用菜单
     * @return
     */
    @PostMapping("/disableMenu")
    @ResponseBody
    public Result disableMenu(@RequestBody DcBaseMenu menu){
        try{
            this.dcSystemBaseMenuService.disableMenu(menu);
            return ResultUtil.success(null, "删除成功");
        }catch(Exception ex){
            return ResultUtil.error("删除失败");
        }
    }


}
